<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>传智健康</title>
        <meta name="description" content="传智健康">
        <meta name="keywords" content="传智健康">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <link rel="stylesheet" href="//unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
        <style>
            .datatable {
                position: relative;
                box-sizing: border-box;
                -webkit-box-flex: 1;
                width: 100%;
                max-width: 100%;
                font-size: 14px;
                color: rgb(96, 98, 102);
                overflow: hidden;
                flex: 1 1 0%;
            }
            .datatable td{
                padding: 12px;
                min-width: 0;
                -webkit-box-sizing: border-box;
                box-sizing: border-box;
                text-overflow: ellipsis;
                vertical-align: middle;
                position: relative;
                text-align: center;
            }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>系统设置<small>菜单管理</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>系统设置</el-breadcrumb-item>
                    <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <div class="box">
                    <div class="filter-container">
                        <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                    </div>

                    <!--<el-tree :data="data1" :props="defaultProps" @node-click="handleNodeClick"></el-tree>-->

                    <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                        <el-table-column type="index" align="center" label="序号"></el-table-column>
                        <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                        <el-table-column prop="linkUrl" label="链接" align="center"></el-table-column>
                       <!-- <el-table-column label="排序" align="center">
                            <template slot-scope="scope">
                                <el-button  size="mini" icon="el-icon-top"></el-button>
                                <el-button  size="mini" icon="el-icon-bottom"></el-button>
                            </template>
                        </el-table-column>-->
                        <el-table-column label="操作" align="center">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <div class="pagination-container">
                        <el-pagination
                            class="pagiantion"
                            @current-change="handleCurrentChange"
                            :current-page="pagination.currentPage"
                            :page-size="pagination.pageSize"
                            layout="total, prev, pager, next, jumper"
                            :total="pagination.total">
                        </el-pagination>
                    </div>
                    <!-- 新增标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                            <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="上级菜单" prop="parentMenuId">
                                            <!--<el-select v-model="formData.parentMenuId" >
                                                <el-option label="无" value=""></el-option>
                                                <el-option label="会员管理" value="1"></el-option>
                                                <el-option label="预约管理" value="5"></el-option>
                                                <el-option label="健康评估" value="11"></el-option>
                                                <el-option label="统计分析" value="13"></el-option>
                                                <el-option label="系统设置" value="15"></el-option>
                                            </el-select>-->
                                            <template>
                                                <el-select v-model="value"  @change="getMenu">
                                                    <el-option label="无" value=""></el-option>
                                                    <el-option
                                                            v-for="item in options"
                                                            :label="item.name"
                                                            :value="item.id">
                                                    </el-option>
                                                </el-select>
                                            </template>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单链接">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </el-form>
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>序号</th>
                                        <th>角色权限</th>
                                        <th>角色权限码</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="c in tableData">
                                        <td>
                                            <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                        </td>
                                        <td><label :for="c.id">{{c.id}}</label></td>
                                        <td><label :for="c.id">{{c.name}}</label></td>
                                        <td><label :for="c.id">{{c.keyword}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible = false">取消</el-button>
                                <el-button type="primary" @click="handleAdd()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>

                    <!-- 编辑标签弹层 -->
                    <div class="add-form">
                        <el-dialog title="新增菜单" :visible.sync="dialogFormVisible4Edit">
                            <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right" label-width="100px">
                                <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="菜单名称" prop="name">
                                            <el-input v-model="formData.name"/>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="上级菜单" prop="parentMenuId">
                                            <el-option label="无" value="null"></el-option>
                                            <!--<el-select v-model="formData.parentMenuId">
                                                <el-option label="无" value="null"></el-option>
                                                <el-option label="会员管理" value="1"></el-option>
                                                <el-option label="预约管理" value="5"></el-option>
                                                <el-option label="健康评估" value="11"></el-option>
                                                <el-option label="统计分析" value="13"></el-option>
                                                <el-option label="系统设置" value="15"></el-option>
                                            </el-select>-->
                                            <template>
                                                <el-select v-model="value"  @change="getMenu">
                                                    <el-option label="无" value="null"></el-option>
                                                    <el-option
                                                            v-for="item in options"
                                                            :label="item.name"
                                                            :value="item.id">
                                                    </el-option>
                                                </el-select>
                                            </template>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="菜单链接">
                                            <el-input v-model="formData.linkUrl"/>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                                <el-row>
                                    <el-col :span="24">
                                        <el-form-item label="描述">
                                            <el-input v-model="formData.description" type="textarea"></el-input>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            <div class="checkScrol">
                                <table class="datatable">
                                    <thead>
                                    <tr>
                                        <th>选择</th>
                                        <th>序号</th>
                                        <th>角色权限</th>
                                        <th>角色权限码</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <tr v-for="c in tableData">
                                        <td>
                                            <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                        </td>
                                        <td><label :for="c.id">{{c.id}}</label></td>
                                        <td><label :for="c.id">{{c.name}}</label></td>
                                        <td><label :for="c.id">{{c.keyword}}</label></td>
                                    </tr>
                                    </tbody>
                                </table>
                            </div>
                            </el-form>
                            <div slot="footer" class="dialog-footer">
                                <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                <el-button type="primary" @click="handleEdit()">确定</el-button>
                            </div>
                        </el-dialog>
                    </div>
                </div>
            </div>
        </div>
    </body>
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script>
        var vue = new Vue({
            el: '#app',
            data:{
                pagination: {
					  currentPage: 1,
					  pageSize:10,
					  total:0,
					  queryString:null
				},
				dataList: [],
                formData: {},
                tableData:[],
                roleIds:[],
                dialogFormVisible: false,
                dialogFormVisible4Edit:false,
                rules: {
                    name: [{ required: true, message: '菜单名称为必填项', trigger: 'blur' }],
                },
                options: [],
                value:''
            },
            /*data1: [{
                label: '一级 1',
                children: [{
                    label: '二级 1-1',
                    children: [{
                        label: '三级 1-1-1'
                    }]
                }]
            }, {
                label: '一级 2',
                children: [{
                    label: '二级 2-1',
                    children: [{
                        label: '三级 2-1-1'
                    }]
                }, {
                    label: '二级 2-2',
                    children: [{
                        label: '三级 2-2-1'
                    }]
                }]
            }, {
                label: '一级 3',
                children: [{
                    label: '二级 3-1',
                    children: [{
                        label: '三级 3-1-1'
                    }]
                }, {
                    label: '二级 3-2',
                    children: [{
                        label: '三级 3-2-1'
                    }]
                }]
            }],
            defaultProps: {
                children: 'children',
                label: 'label'
            },*/

            created() {
                this.findPage();
            },
            methods: {
                getMenu(val){
                    this.formData.parentMenuId= val;
                    //获取档案名
                   /* let obj = {};
                    obj = this.options.find((item)=>{
                        return item.fileNumber === val;
                    });

                    this.formData.mname=obj.name;
                    this.formData.checkDate=obj.orderDate;*/

                },
                /*handleNodeClick(data1) {
                    console.log(data1);
                },*/
                //编辑
                handleEdit() {
                    //进行表单校验
                    this.$refs['dataEditForm'].validate((valid) => {
                        if(valid){
                            //如果校验通过，发送ajax请求，将表单数据提交到Controller
                            axios.post("/menu/edit.do?roleIds="+this.roleIds,this.formData).then((res) => {
                                if(res.data.flag){
                                    //处理成功
                                    this.$message({
                                        type:'success',
                                        message:res.data.message
                                    });
                                    //关闭新增窗口
                                    this.dialogFormVisible4Edit = false;
                                    //分页查询
                                    this.findPage();
                                }else{
                                    //处理失败
                                    this.$message.error(res.data.message);
                                }
                            });
                        }else{
                            //校验不通过，提示错误信息
                            this.$message.error("表单数据校验失败，请检查输入是否正确！");
                        }
                    });
                },
                //添加
                handleAdd () {
                    //进行表单校验
                    this.$refs['dataAddForm'].validate((valid) => {
                        if(valid){
                            //如果校验通过，发送ajax请求，将表单数据提交到Controller
                            axios.post("/menu/add.do?roleIds="+this.roleIds,this.formData).then((res) => {
                                if(res.data.flag){
                                    //处理成功
                                    this.$message({
                                        type:'success',
                                        message:res.data.message
                                    });
                                    //关闭新增窗口
                                    this.dialogFormVisible = false;
                                    //分页查询
                                    this.findPage();
                                }else{
                                    //处理失败
                                    this.$message.error(res.data.message);
                                }
                            });
                        }else{
                            //校验不通过，提示错误信息
                            this.$message.error("表单数据校验失败，请检查输入是否正确！");
                        }
                    });
                },
                //分页查询
                findPage() {
                    var param = {
                        currentPage:this.pagination.currentPage,
                        pageSize:this.pagination.pageSize
                    };

                    //发送ajax请求，进行分页查询
                    axios.post("/menu/findPage.do",param).then((res) => {
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                    });

                },
                // 重置表单
               resetForm() {
                    this.formData = {};
                    this.roleIds=[];
                    this.value = ''
                },
                // 弹出添加窗口
                handleCreate() {
                    this.resetForm();
                    //将添加窗口展示出来
                    this.dialogFormVisible = true;
                    axios.get("/role/findAllRole.do").then((res)=>{
                        this.tableData = res.data.data;
                    });
                    axios.post("/menu/findOneMenu.do").then((response)=>{
                        this.options=response.data.data;
                    });
                },
                // 弹出编辑窗口
                handleUpdate(row) {
                    this.dialogFormVisible4Edit = true;
                    //重置表单
                    this.resetForm();
                    axios.post("/menu/findOneMenu.do").then((response)=>{
                        this.options=response.data.data;
                    });
                    axios.get("/menu/findById.do?id=" + row.id).then((res) => {
                        if(res.data.flag){
                            this.formData = res.data.data;//为模型数据赋值，基于VUE数据绑定进行回显
                            /*this.formData.parentMenuId = res.data.data.parentMenuId+""*/
                            this.value = res.data.data.parentMenuId+""
                            //alert(this.value)
                            /*if (this.value='null'){
                                this.value = ''
                            }else {
                                this.value = res.data.data.parentMenuId+""
                            }*/

                        }
                    });
                    axios.get("/role/findAllRole.do").then((res) => {
                        if(res.data.flag){
                            this.tableData = res.data.data;
                            axios.get("/role/findRolesByMenuId.do?menuId=" + row.id).then((res) => {
                                if(res.data.flag){
                                    this.roleIds = res.data.data;
                                }
                            });
                        }else{
                            this.$message.error(res.data.message);
                        }
                    });
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    this.pagination.currentPage = currentPage;//指定最新的页码
                    this.findPage();
                },

                /*showMessage(r){
                    if(r == 'Error: Request failed with status code 403'){
                        //权限不足
                        this.$message.error('无访问权限');
                        return;
                    }else{
                        this.$message.error('未知错误');
                        return;
                    }
                },*/

                // 删除
                handleDelete(row) {
                    //弹出确认框
                    this.$confirm('你确定要删除当前数据吗？','提示',{
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        //发送ajax请求，将id提交到Controller
                        axios.get("/menu/delete.do?id=" + row.id).then((res) => {
                            if(res.data.flag){
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                this.findPage();
                            }else{
                                //执行失败
                                this.$message.error(res.data.message);
                            }
                        }).catch((r) => {
                            this.showMessage(r);
                        });
                    }).catch(() => {
                        this.$message("已取消");
                    });
                }
            }
        })
    </script>
</html>